﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Text.RegularExpressions;

namespace SupplyChainManagement
{
    /// <summary>
    /// Form to add a new product
    /// </summary>
    public partial class ProductAddForm : Form
    {
        List<string> rawitemData;
        List<string> selectedItems = new List<string>();
        ProductForm prod;

        public string ProductID
        {
            get { return txtID.Text; }
        }

        public string ProdName
        {
            get { return txtPName.Text; }
        }

        public ProductAddForm(ProductForm prod)
        {
            this.prod = prod;
            InitializeComponent();
            rawitemData = new List<string>();
            LostListBox();
        }

        //populate listbox with data from inventory table
        private void LostListBox()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            
            try
            {
                //open database
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                //assign the SQL command
                sqlCommand.CommandText = "select inv_name from inventory";

                //reader executes the select command
                OdbcDataReader sqlReader = sqlCommand.ExecuteReader();          
                while (sqlReader.Read())
                {
                    rawitemData.Add(sqlReader.GetString(0));
                }

                //close readers and dispose the commands
                sqlReader.Close();
                sqlCommand.Dispose();
                rawItemListBox.Items.AddRange(rawitemData.ToArray());
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            selectedItems.Clear();
            foreach (string s in rawItemListBox.SelectedItems)
            {
                selectedItems.Add(s.ToString());
            }
            if (string.IsNullOrEmpty(txtAmount.Text) || string.IsNullOrEmpty(txtPName.Text) ||
                string.IsNullOrEmpty(txtMin.Text) || string.IsNullOrEmpty(txtMax.Text))
                MessageBox.Show("Data is not complete");
            //else if (selectedItems.Count == 0)
            //    MessageBox.Show("No raw item selected!");
            else if (!Regex.IsMatch(txtAmount.Text, @"^[0-9,-\.]+$") && !Regex.IsMatch(txtMin.Text, @"^[0-9,-\.]+$")
                && !Regex.IsMatch(txtMax.Text, @"^[0-9,-\.]+$"))
                MessageBox.Show("Data not valid!");
            else
            {
                
                ProductAddFormNext next = new ProductAddFormNext(selectedItems, this, prod);
                next.Show();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            prod.Enabled = true;
            prod.Activate();
        }

        public void SaveToDatabase()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                OdbcCommand rawItemCmd = dbMySQL.CreateCommand();
                rawItemCmd.CommandText = "insert into product value ('" + txtID.Text + "','" + txtPName.Text + "','" +
                    txtAmount.Text + "','" + txtMax.Text + "','" + txtMin.Text + "')" ;
                rawItemCmd.ExecuteNonQuery();
                rawItemCmd.Dispose();
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void ProductAddForm_FormClosed_1(object sender, FormClosedEventArgs e)
        {
            prod.Enabled = true;
            prod.Activate();
        }

    }
}
